package com.macro.mall.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.mapper.PmsProductMapper;
import com.macro.mall.model.PmsProduct;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.domain.MemberReadHistory;
import com.macro.mall.portal.dto.PmsProductDto;
import com.macro.mall.portal.repository.MemberReadHistoryRepository;
import com.macro.mall.portal.service.MemberReadHistoryService;
import com.macro.mall.portal.service.PmsPortalProductService;
import com.macro.mall.portal.service.UmsMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员浏览记录管理Service实现类
 * Created by macro on 2018/8/3.
 */
@Service
public class MemberReadHistoryServiceImpl implements MemberReadHistoryService {
    @Autowired
    private MemberReadHistoryRepository memberReadHistoryRepository;
    @Autowired
    private UmsMemberService memberService;
    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private PmsPortalProductService productService;

    @Override
    public int create(MemberReadHistory memberReadHistory) {
        if (memberReadHistory.getProductId() == null) {
            return 1;
        }
        //当天重复的商品id不添加
        Page<MemberReadHistory> page = this.list(1, 5);
        List<MemberReadHistory> list = page.getContent();
        //当天的浏览记录
        List<MemberReadHistory> histories = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(e->{
                String createDate = sdf.format(e.getCreateTime());
                if(date.equals(createDate)){
                    histories.add(e);
                }
            });
            if(!CollectionUtils.isEmpty(histories)){
                List<Long> collect = histories.stream().map(MemberReadHistory::getProductId).collect(Collectors.toList());
                if (collect.contains(memberReadHistory.getProductId())) {
                    return 1;
                }
            }
        }
        UmsMember member = memberService.getCurrentMember();
        memberReadHistory.setMemberId(member.getId());
        memberReadHistory.setMemberUsername(member.getUsername());
        memberReadHistory.setMemberIcon(member.getIcon());
        memberReadHistory.setId(null);
        memberReadHistory.setCreateTime(new Date());
        //商品信息
        PmsProduct product = productMapper.selectByPrimaryKey(memberReadHistory.getProductId());
        //如果商品下架或者被删除，不创建
        if (product.getDeleteStatus().equals(1) || product.getPublishStatus().equals(0)) {
            return 1;
        }
        memberReadHistory.setProductSale(product.getSale());
        memberReadHistory.setProductOriginalPrice(product.getOriginalPrice());
        memberReadHistory.setProductName(product.getName());
        memberReadHistory.setProductPic(product.getPic());
        memberReadHistory.setProductSubTitle(product.getSubTitle());
        memberReadHistory.setProductPrice(product.getPrice());
        List<PmsProduct> products = new ArrayList<>();
        products.add(product);
        List<PmsProductDto> memberPrice = productService.getMemberPrice(products, member.getStoreId());
        memberReadHistory.setProductMemberPrice(memberPrice.get(0).getMemberPrice());
        memberReadHistoryRepository.save(memberReadHistory);
        return 1;
    }

    @Override
    public int delete(List<String> ids) {
        List<MemberReadHistory> deleteList = new ArrayList<>();
        for (String id : ids) {
            MemberReadHistory memberReadHistory = new MemberReadHistory();
            memberReadHistory.setId(id);
            deleteList.add(memberReadHistory);
        }
        memberReadHistoryRepository.deleteAll(deleteList);
        return ids.size();
    }

    @Override
    public Page<MemberReadHistory> list(Integer pageNum, Integer pageSize) {
        UmsMember member = memberService.getCurrentMember();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<MemberReadHistory> list = memberReadHistoryRepository.findByMemberIdOrderByCreateTimeDesc(member.getId(), pageable);
        return list;
    }

    @Override
    public CommonPage<MemberReadHistory> getList(Long memberId, String date, Integer pageNum, Integer pageSize) {
        List<MemberReadHistory> result = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        UmsMember member = memberService.getCurrentMember();
        List<MemberReadHistory> memberReadHistories = memberReadHistoryRepository.findByMemberIdOrderByCreateTimeDesc(memberId);
        PageHelper.startPage(pageNum, pageSize);
        //处理分页
        CommonPage<MemberReadHistory> memberReadHistoriesPage = CommonPage.restPage(memberReadHistories);
        List<MemberReadHistory> memberReadHistoriesList = memberReadHistoriesPage.getList();
        for (MemberReadHistory memberReadHistory : memberReadHistoriesList) {
            String createTime = sdf.format(memberReadHistory.getCreateTime());
            if (createTime.equals(date)) {
                result.add(memberReadHistory);
            }
        }
        CommonPage<MemberReadHistory> resultPage = new CommonPage<>();
        BeanUtils.copyProperties(memberReadHistoriesPage, resultPage);
        resultPage.setList(result);
        return resultPage;
    }

    @Override
    public void clear() {
        UmsMember member = memberService.getCurrentMember();
        memberReadHistoryRepository.deleteAllByMemberId(member.getId());
    }
}
